package com.xtq.countclass.controller;

import com.xtq.countclass.entity.Groupe;
import com.xtq.countclass.entity.User;

import com.xtq.countclass.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生表;(User)表控制层
 *
 * @author makejava
 * @since 2024-03-06 00:10:28
 */
@RestController
@RequestMapping("user")
public class UserController {
	/**
	 * 服务对象
	 */
	@Resource
	private UserService userService;
	
	
	/**
	 * 测试接口
	 */
	@GetMapping("/test")
	public String test() {
		return "get请求测试成功";
	}
	
	/**
	 * 分页查询
	 * @param id        根据组id筛选
	 * @return 查询结果
	 */
	@GetMapping("/queryByGroup/{id}")
	public ResponseEntity<List<User>> queryByGroup(@PathVariable String id) {
		return ResponseEntity.ok(this.userService.queryByGroupID(id));
	}
	
	/**
	 * @param user 用户密码用户名
	 * @return 单条数据
	 */
	@PostMapping("/login")
	public ResponseEntity<Boolean> login(@RequestBody User user) {
		// 查到是admin才登
		return ResponseEntity.ok(this.userService.isAdmin(user.getId(), user.getPassword()));
	}
	
	/**
	 * 新增数据
	 *
	 * @param user 实体
	 * @return 新增结果
	 */
	@PostMapping("/add")
	public ResponseEntity<Boolean> add(@RequestBody User user) {
		
		try {
			return ResponseEntity.ok( this.userService.insert(user)!=null);
		} catch (Exception e) {
			return ResponseEntity.ok(false);
		}
	}
	
	/**
	 * 编辑数据
	 *
	 * @param user 实体
	 * @return 编辑结果
	 */
	@PutMapping
	public ResponseEntity<User> edit(User user) {
		return ResponseEntity.ok(this.userService.update(user));
	}
	
	/**
	 * 删除数据
	 *
	 * @param id 主键
	 * @return 删除是否成功
	 */
	@DeleteMapping("/del/{id}")
	public ResponseEntity<Boolean> delete(@PathVariable String id) {
		return ResponseEntity.ok(this.userService.deleteById(id));
	}
	@GetMapping("/modifyPassword/{pwd}")
	public ResponseEntity<Boolean> modifyPassword(@PathVariable String pwd) {
		return ResponseEntity.ok(this.userService.modifyPassword(pwd));
	}

}
